const crypto = require("crypto");
const axios = require("axios");

const { headers } = require("./options");

const secret =
    "SEC2fe7caaf3dd1ae059102f79b40c1877d0788c5055a36c1d21af903f1bd6acf91";
const urlPart =
    "https://oapi.dingtalk.com/robot/send?access_token=b6eb822108a8e6053138c4ce3e6c422e322a82253a88d56c750c21f9753e0fe0";

const timestamp = `${Date.now()}`;

const secretEncode = Buffer.from(secret).toString();

const stringToSign = `${timestamp}\n${secret}`;

const stringToSignEncode = Buffer.from(stringToSign).toString();

// 使用HmacSHA256算法和密钥进行加签
const signature = crypto
    .createHmac("sha256", secretEncode)
    .update(stringToSignEncode)
    .digest("base64");

// 消息内容（假设为JSON格式）
const message = {
    msgtype: "text",
    text: {
        content: "Hello, this is a signed message!",
    },
};

const urlWithSign = `${urlPart}&timestamp=${timestamp}&sign=${encodeURIComponent(
    signature
)}`;

// 发送消息
const sendMessage = () => {
    return axios.post(urlWithSign, message);
};

axios
    .get("https://c.ss.lenovo.com/api/v1/mall/getProducts", {
        headers,
        httpsAgent: new https.Agent({ keepAlive: true }),
    })
    .then((response) => {
        console.log("消息发送成功", response.data);
    })
    .catch((error) => {
        console.error("消息发送失败", error);
    });
// const checkPuduct = () => {};
